package com.huikeportal.controller;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huikeportal.controller.common.Tools;
import com.huikeportal.model.DpModel;
import com.huikeportal.model.MulSiteCondition;
import com.huikeportal.model.QyModel;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteSelectParam;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.common.SqlParameter;
import com.huikeportal.service.LoginService;
import com.huikeportal.service.SelectSiteService;
import com.huikeportal.service.UserService;

@Controller
public class MulChoiceSiteController {

	@Autowired
	private HttpSession session;

	@Autowired
	private LoginService loginService;

	@Autowired
	private SelectSiteService selectSiteService;

	@Autowired
	private UserService userService;

	@Autowired
	private HttpServletRequest req;

	@InitBinder("siteSelectParam")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("siteSelectParam.");
	}

	@RequestMapping("/mulChoiceLoad")
	public String mulChoiceLoad(SiteSelectParam siteSelectParam, ModelMap model)
			throws Exception {
		// 两种传参方式：form表单提交、转发跳转
		if ("".equals(siteSelectParam.getControlTo())
				|| siteSelectParam.getControlTo() == null) {
			siteSelectParam = (SiteSelectParam) (req
					.getAttribute("siteSelectParam"));
		}
		// 如果参数为空，直接返回登录界面
		if ("".equals(siteSelectParam.getControlTo())
				|| siteSelectParam.getControlTo() == null) {
			return "index";
		}
		MulSiteCondition mulSiteCondition = new MulSiteCondition();
		// 如果是从更换广场页面传过来
		if (!"".equals(siteSelectParam.getBackGcSiteString())
				&& siteSelectParam.getBackGcSiteString() != null) {
			String tabeId = siteSelectParam.getBackTabId();
			List<SiteAllInfo> gcList = userService
					.getSiteByString(siteSelectParam.getBackGcSiteString());
			// 如果是区域tab更换广场
			if ("2".equals(tabeId)) {
				mulSiteCondition.setQy_gcList(gcList);
			}
			// 如果是楼层tab更换广场
			else if ("3".equals(tabeId)) {
				mulSiteCondition.setLc_gcList(gcList);
			}
			// 如果是店铺tab更换广场
			else if ("4".equals(tabeId)) {
				mulSiteCondition.setDp_gcList(gcList);
			}
			model.addAttribute("backFrom", "1");// 从更换广场回来
		}
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		userInfo = loginService.getUserInfo(userInfo);
		String menuHtml = loginService.getMenuHtml(userInfo, siteSelectParam
				.getPageFrom());
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		// 获取登录用户集团场所权限
		List<SiteAllInfo> jtList = loginService.getJtSiteList(userInfo);
		if (jtList.size() == 0
				|| "userManager".equals(siteSelectParam.getPageFrom())) {
			model.addAttribute("isHaveJt", "0");
		} else {
			model.addAttribute("isHaveJt", "1");
		}
		// 获取登录用户广场场所权限
		List<SiteAllInfo> gcList = loginService.getGcSiteList(userInfo);
		if (gcList.size() == 0) {
			model.addAttribute("isHaveGc", "0");
		} else {
			model.addAttribute("isHaveGc", "1");
		}
		// 获取登录用户区域场所权限
		List<SiteAllInfo> qyList = loginService.getQySiteList(userInfo);
		if (qyList.size() == 0
				|| "userManager".equals(siteSelectParam.getPageFrom())) {
			model.addAttribute("isHaveQy", "0");
		} else {
			model.addAttribute("isHaveQy", "1");
		}
		// 获取登录用户楼层场所权限
		List<SiteAllInfo> lcList = loginService.getLcSiteList(userInfo);
		if (lcList.size() == 0
				|| "userManager".equals(siteSelectParam.getPageFrom())) {
			model.addAttribute("isHaveLc", "0");
		} else {
			model.addAttribute("isHaveLc", "1");
		}
		// 获取登录用户店铺场所权限
		List<SiteAllInfo> dpList = loginService.getDpSiteList(userInfo);
		if (dpList.size() == 0) {
			model.addAttribute("isHaveDp", "0");
		} else {
			model.addAttribute("isHaveDp", "1");
		}
		mulSiteCondition.setJtList(jtList);
		mulSiteCondition.setGcList(gcList);
		mulSiteCondition.setQyList(qyList);
		mulSiteCondition.setLcList(lcList);
		mulSiteCondition.setDpList(dpList);
		mulSiteCondition.setRangeType(siteSelectParam.getRangeType());
		// 获取所有筛选条件初始化
		mulSiteCondition = selectSiteService.mulSelectLoad(mulSiteCondition);
		model.addAttribute("mulSiteCondition", mulSiteCondition);
		model.addAttribute("siteSelectParam", siteSelectParam);
		if ("comparativeAnalysis".equals(siteSelectParam.getControlTo())) {
			model.addAttribute("pageHead", "对比分析");
		} else if ("squareWeek".equals(siteSelectParam.getControlTo())) {
			model.addAttribute("pageHead", "星期分析");
		} else if ("squareWeather".equals(siteSelectParam.getControlTo())) {
			model.addAttribute("pageHead", "天气分析");
		} else {
			model.addAttribute("pageHead", "用户管理");
		}
		if (!"".equals(siteSelectParam.getSiteString())) {
			List<SiteAllInfo> allSiteList = userService
					.getSiteByString(siteSelectParam.getSiteString());
			List<SiteAllInfo> jtSite = new ArrayList<SiteAllInfo>();
			List<SiteAllInfo> qySite = new ArrayList<SiteAllInfo>();
			List<SiteAllInfo> gcSite = new ArrayList<SiteAllInfo>();
			List<SiteAllInfo> lcSite = new ArrayList<SiteAllInfo>();
			List<SiteAllInfo> dpSite = new ArrayList<SiteAllInfo>();
			for (int i = 0; i < allSiteList.size(); i++) {
				if (allSiteList.get(i).getType() == 100) {
					jtSite.add(allSiteList.get(i));
				} else if (allSiteList.get(i).getType() == 300) {
					gcSite.add(allSiteList.get(i));
				} else if (allSiteList.get(i).getType() == 400) {
					qySite.add(allSiteList.get(i));
				} else if (allSiteList.get(i).getType() == 500) {
					lcSite.add(allSiteList.get(i));
				} else if (allSiteList.get(i).getType() == 600) {
					dpSite.add(allSiteList.get(i));
				}
			}
			model.addAttribute("load_Site1", Tools.pinjieId(jtSite));
			model.addAttribute("load_Site2", Tools.pinjieId(gcSite));
			model.addAttribute("load_Site3", Tools.pinjieId(qySite));
			model.addAttribute("load_Site4", Tools.pinjieId(lcSite));
			model.addAttribute("load_Site5", Tools.pinjieId(dpSite));
		}
		if (!"".equals(siteSelectParam.getBackTabId())
				&& siteSelectParam.getBackTabId() != null) {
			model.addAttribute("tabId", siteSelectParam.getBackTabId());
		}
		return "selectMulSite";
	}

	/**
	 * ajax请求查询集团
	 * 
	 * @param jtSiteName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/searchjtSiteMul")
	@ResponseBody
	public Map<String, Object> searchjtSiteMul(String jtSiteName)
			throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		userInfo = loginService.getUserInfo(userInfo);
		Map<String, Object> map = new HashMap<String, Object>();
		// 获取用户的集团权限
		List<SiteAllInfo> jtList = loginService.getJtSiteList(userInfo);
		List<SiteAllInfo> jtSiteList = new ArrayList<SiteAllInfo>();
		if ("".equals(jtSiteName) || jtSiteName == null) {
			jtSiteList.addAll(jtList);
		} else {
			for (int i = 0; i < jtList.size(); i++) {
				jtSiteName = URLDecoder.decode(jtSiteName, "UTF-8");
				if (jtList.get(i).getSitename().indexOf(jtSiteName) != -1) {
					jtSiteList.add(jtList.get(i));
				}
			}
		}
		map.put("jtList", jtSiteList);
		return map;
	}

	/**
	 * 国家省市区联动
	 * 
	 * @param type
	 *            类型
	 * @param id
	 *            id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/searchCombo")
	@ResponseBody
	public Map<String, Object> searchCombo(String type, String id)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<SiteAllInfo> siteList = selectSiteService.searchCombo(type, id);
		map.put("siteList", siteList);
		return map;
	}

	/**
	 * ajax请求区域信息
	 * 
	 * @param gcSiteId
	 * @param lcSiteId
	 * @param qySiteName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/searchQySiteMul")
	@ResponseBody
	public Map<String, Object> searchQySiteMul(String gcSiteId,
			String lcSiteId, String qySiteName) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		QyModel qyModel = new QyModel();
		qyModel.setGcSiteId(gcSiteId);
		if (!"".equals(lcSiteId) && !"-1".equals(lcSiteId)) {
			lcSiteId = URLDecoder.decode(lcSiteId, "UTF-8");
			qyModel.setLcSiteName(lcSiteId);
		}
		if (!"".equals(qySiteName) && qySiteName != null) {
			qySiteName = URLDecoder.decode(qySiteName, "UTF-8");
			qyModel.setQySiteName(qySiteName);
		}
		List<SiteAllInfo> qyList = selectSiteService.mulSelectQy(qyModel);
		map.put("qyList", qyList);
		return map;
	}

	/**
	 * ajax请求楼层数据
	 * 
	 * @param gcSiteId
	 *            广场场所id拼接字符串
	 * @param lcAName
	 *            楼层名称(筛选)
	 * @param lcSiteName
	 *            搜索栏中楼层名称
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/searchLcSiteMul")
	@ResponseBody
	public Map<String, Object> searchLcSiteSingle(String gcSiteId,
			String lcAName, String lcSiteName) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		SqlParameter sql = new SqlParameter();
		sql.setString(gcSiteId);
		if (!"".equals(lcSiteName) && !"-1".equals(lcSiteName)) {
			lcSiteName = URLDecoder.decode(lcSiteName, "UTF-8");
			sql.setStringChar(lcSiteName);
		}
		if (!"".equals(lcAName) && !"-1".equals(lcAName)) {
			lcAName = URLDecoder.decode(lcAName, "UTF-8");
			sql.setStringName(lcAName);
		}
		List<SiteAllInfo> lcList = selectSiteService.mulSelectLc(sql);
		map.put("lcList", lcList);
		return map;
	}

	/**
	 * ajax请求店铺数据
	 * 
	 * @param gc
	 *            广场场所id拼接字符串
	 * @param pp
	 *            品牌id
	 * @param lc
	 *            楼层名称
	 * @param dpName
	 *            店铺名称
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/searchdpSiteMul")
	@ResponseBody
	public Map<String, Object> searchdpSiteMul(String gc, String pp, String lc,
			String dpName) throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		Map<String, Object> map = new HashMap<String, Object>();
		// 获取用户的店铺权限
		List<SiteAllInfo> dpSiteList = loginService.getDpSiteList(userInfo);
		String dpString = "";
		for (int i = 0; i < dpSiteList.size(); i++) {
			dpString += dpSiteList.get(i).getSiteid() + ",";
		}
		if (!"".equals(dpString)) {
			dpString = dpString.substring(0, dpString.length() - 1);
		}
		DpModel dpModel = new DpModel();
		dpModel.setDpString(dpString);// 设置店铺查询范围
		dpModel.setGcString(gc);// 设置广场查询范围
		if (!"".equals(pp) && !"-1".equals(pp)) {
			dpModel.setPpId(pp);
		}
		if (!"".equals(lc) && !"-1".equals(lc)) {
			lc = URLDecoder.decode(lc, "UTF-8");
			dpModel.setLcSiteName(lc);
		}
		if (!"".equals(dpName) && !"-1".equals(dpName)) {
			dpName = URLDecoder.decode(dpName, "UTF-8");
			dpModel.setDpSiteName(dpName);
		}
		List<SiteAllInfo> dpList = selectSiteService.mulSelectDp(dpModel);
		map.put("dpList", dpList);
		return map;
	}

}
